import 'babel-polyfill'
import Vue from 'vue'

import 'normalize.css/normalize.css' // A modern alternative to CSS resets

import dataV from '@jiaminghi/data-view'
import VScaleScreen from 'v-scale-screen'
import '@/styles/iconfont/iconfont.css'
import Element from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import 'font-awesome/css/font-awesome.min.css'
Element.Dialog.props.modal.default = false

import '@/styles/index.scss' // global css
import '@/styles/layout.scss'

import App from './App'
import router from './router'
import store from './store'

// 下载表格，全局注册toExcel方法
import toExcel from '@/excel/json2excel'
Vue.prototype.$toExcel = toExcel

import OssUploader from './utils/ossuploader'
import ReaderD8 from './utils/readerd8'
import ReaderHd100 from './utils/readerHd100'
import ReaderHd900 from './utils/readerHd900'
import Anychat from './utils/anychat'
import './utils/uuid'

import './icons' // icon
import './errorLog' // error log
import './permission' // permission control
import Permission from './directive/permission'
// import './mock' // simulation data

import * as filters from './filters' // global filters

import echarts from 'echarts'

Permission.install(Vue)
// 加载系统字典
// const dictsJs = document.createElement('script')
// dictsJs.setAttribute('id', 'dicts')
// dictsJs.type = 'text/javascript'
// document.head.appendChild(dictsJs)
// dictsJs.src = process.env.BASE_API + 'static/js/SM_DICT.js?r=' + Math.random()
// dictsJs.onload = () => {
//   // 全局设置字典
// }
Vue.prototype.Dicts = Dicts

Vue.prototype.$echarts = echarts
Vue.use(dataV)
Vue.use(VScaleScreen)
Vue.use(OssUploader)
Vue.use(ReaderD8)
Vue.use(ReaderHd100)
Vue.use(ReaderHd900)
Vue.use(Anychat)
Vue.use(Element, {
  size: 'medium'
})

// register global utility filters.
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})

Vue.config.productionTip = false

global.Form = (data, opt = { ignore: true }) => {
  let queryString = ''
  const form =
    typeof URLSearchParams !== 'undefined'
      ? new URLSearchParams()
      : new FormData()
  if (data) {
    for (const i in data) {
      if ((data[i] === null || data[i] === undefined) && opt.ignore) continue
      if (data[i] instanceof Array) {
        for (let j = 0; j < data[i].length; j++) {
          queryString =
            queryString +
            (i +
              (opt.arrayKey || '') +
              '=' +
              (data[i][j] === undefined ? '' : data[i][j]) +
              '&')
          form.append(
            i + (opt.arrayKey || ''),
            data[i][j] === undefined ? '' : data[i][j]
          )
        }
      } else {
        queryString = queryString + (i + '=' + data[i] + '&')
        form.append(i, data[i] === undefined ? '' : data[i])
      }
    }
  }
  form.queryString = _ => {
    return queryString.length > 0
      ? queryString.substr(0, queryString.length - 1)
      : ''
  }
  return form
}

global.Vue = Vue
global._vm = new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
